package com.example.house.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.house.R
import com.example.house.databinding.ItemHouseOneBinding
import com.example.house.databinding.ItemHouseTwoBinding
import com.example.house.entity.House
import com.example.house.entity.HouseEntity

class HouseOneAdapter(private val data:List<HouseEntity>):RecyclerView.Adapter<HouseOneAdapter.HouseOneViewHolder>() {

//    private var itemOnClickListener:ItemOnClickListener? = null
//
//    fun setItemOnClickListener(listener:ItemOnClickListener){
//        this.itemOnClickListener = listener
//    }

    inner class HouseOneViewHolder(val itemDataBinding:ItemHouseOneBinding):RecyclerView.ViewHolder(itemDataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HouseOneViewHolder {
        return HouseOneViewHolder(ItemHouseOneBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: HouseOneViewHolder, position: Int) {
        val item = data[position]
        holder.itemDataBinding.tvLou.text = "${item.lou}楼"
        holder.itemDataBinding.tvArea.text = "${item.area}m²"
        val rv = holder.itemDataBinding.rv01
        val img = holder.itemDataBinding.more01
        img.setOnClickListener {
            if(rv.visibility == View.GONE){
                rv.visibility = View.VISIBLE
                img.setImageResource(com.example.resource.R.drawable.icon_more_on)
            }else{
                rv.visibility = View.GONE
                img.setImageResource(com.example.resource.R.drawable.dwon_more)
            }
        }
        val houseTwoAdapter = HouseTwoAdapter(item.house)
        rv.adapter =houseTwoAdapter
        houseTwoAdapter.notifyDataSetChanged()
//        holder.itemDataBinding.root.setOnClickListener {
//            itemOnClickListener?.onClick(it,position)
//        }
    }

    interface ItemOnClickListener{
        fun onClick(view: View,position: Int)
    }
}
class HouseTwoAdapter(private val data:List<House>):RecyclerView.Adapter<HouseTwoAdapter.HouseTwoViewHolder>(){
    inner class HouseTwoViewHolder(val itemDataBinding:ItemHouseTwoBinding):RecyclerView.ViewHolder(itemDataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HouseTwoViewHolder {
        return HouseTwoViewHolder(ItemHouseTwoBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: HouseTwoViewHolder, position: Int) {
        val entity = data[position]
        holder.itemDataBinding.areaTwo.text = "面积: ${entity.area}m²"
        holder.itemDataBinding.tvName.text = "企业: ${entity.regionName}"
        holder.itemDataBinding.tvId.text = "${entity.id}室"
        if(entity.rented){
            holder.itemDataBinding.tvStatus.text = "出租"
            holder.itemDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_house_true)
            holder.itemDataBinding.tvStatus.setTextColor(holder.itemDataBinding.tvStatus.resources.getColor(com.example.resource.R.color.houseGreen))
        }else {
            holder.itemDataBinding.tvStatus.text = "空置"
            holder.itemDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_house_false)
            holder.itemDataBinding.tvStatus.setTextColor(holder.itemDataBinding.tvStatus.resources.getColor(com.example.resource.R.color.houseBlue))
        }
    }
}